#!/bin/sh

# if app not exist
if [ ! -f /bin/smbd ] || [ ! -f /bin/nmbd ]; then
    exit 0
fi

# get params
. /etc/scripts/global.sh

LOG="logger -t samba"

start() {
SmbEnabled=`nvram_get 2860 SmbEnabled`
if [ "$SmbEnabled" = "1" ]; then
    get_param

    echo "[global]" > $CONFIG
    # for file share mode need increase buffers size
    echo "    interfaces = $interfaces" >> $CONFIG
    echo "    socket options = $socket_options" >> $CONFIG
    echo "    workgroup = $WorkGroup" >> $CONFIG
    echo "    netbios name = $SmbNetBIOS" >> $CONFIG
    echo "    server string = $SmbString" >> $CONFIG
    echo "    os level = $SmbOsLevel" >> $CONFIG
    echo "    force user = $Login" >> $CONFIG
    echo "    time server = $ntp_support" >> $CONFIG

    # external wins register support
    if [ -f /tmp/wins.dhcp ]; then
	echo "name resolve order		= lmhosts host wins bcast"	>> $CONFIG
	wins_srvs=`cat /tmp/wins.dhcp | tail -n1`
	echo "wins server			= $wins_srvs"			>> $CONFIG
    else
	echo "name resolve order		= lmhosts host bcast"		>> $CONFIG
    fi

    # check samba version
    if [ "$is_smb_v3" != "0" ]; then
	echo "    hosts allow = $lan_ipaddr/$lan_netmask" >> $CONFIG
    fi

    # add template to config
    cat $TEMPLATE >> $CONFIG

    # share /media
    if [ -d /proc/bus/usb ]; then
	if [ $public != "" ]; then
	    echo "[public]" >> $CONFIG
	    echo "    comment = pub" >> $CONFIG
	    echo "    path = $public" >> $CONFIG
	    echo "    public = yes" >> $CONFIG
	    echo "    browseable = yes" >> $CONFIG
	    echo "    guest ok = yes" >> $CONFIG
	    echo "    guest only = yes" >> $CONFIG
	    echo "    writable = yes" >> $CONFIG
	    echo "" >> $CONFIG
	fi
    fi

    if [ -f /var/lock/smbd.pid ] || [ -f /var/lock/nmbd.pid ]; then
	killall -q smbd
	killall -q nmbd
	killall -q -SIGKILL smbd
	killall -q -SIGKILL nmbd
    fi
    $LOG "Starting WINS/SMB SERVER."
    find /var/log -type f -name "*mbd*" | while read samba_log; do
	echo > "$samba_log"
    done
    nmbd -s /etc/smb.conf -d0 -D &
    nice -10 smbd -s /etc/smb.conf -d0 -D &
fi
}


get_param() {
    eval `nvram_buf_get 2860 WorkGroup SmbNetBIOS SmbString SmbOsLevel SmbTimeserver NTPEnabled NTPServerIP Login lan_ipaddr lan_netmask`
    CONFIG="/etc/smb.conf"
    TEMPLATE="/etc/smb.conf.template"

    if [ "$SmbTimeserver" = "1" ] && [ "$NTPEnabled" = "on" ] && [ "$NTPServerIP" != "" ]; then
	ntp_support="yes"
    else
	ntp_support="no"
    fi
    if [ -d /proc/bus/usb ]; then
	socket_options="SO_BROADCAST IPTOS_THROUGHPUT TCP_NODELAY SO_RCVBUF=65536 SO_SNDBUF=65536" >> $CONFIG
    else
	socket_options="SO_BROADCAST IPTOS_THROUGHPUT TCP_NODELAY SO_RCVBUF=4096 SO_SNDBUF=4096" >> $CONFIG
    fi

    interfaces="lo 127.0.0.1/255.0.0.0 $lan_if $lan_ipaddr/$lan_netmask"

    is_smb_v3=`grep -c "NTFS" < $TEMPLATE`
}

stop() {
    if [ -f /var/lock/smbd.pid ] || [ -f /var/lock/nmbd.pid ]; then
     $LOG "Stop WINS/SMB SERVER."
      killall -q nmbd
      killall -q smbd
      killall -q -SIGKILL nmbd
      killall -q -SIGKILL smbd
      rm -f /var/lock/*mbd.pid > /dev/null 2>&1
    fi
}

case "$1" in
        start)
            start
            ;;

	stop)
            stop
            ;;

	restart)
            stop
            start
            ;;

        *)
            echo $"Usage: $0 {start|stop|restart}"
            exit 1
esac
